package cn.kevinkun.WheelView;

import java.util.ArrayList;
import java.util.List;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.HVArrangement;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.LinearLayout;

@DesignerComponent(version = 2, category = ComponentCategory.EXTENSION, description = "WheelView", iconName = "images/extension.png", nonVisible = true)
@SimpleObject(external = true)
public class KevinkunWheelView extends AndroidNonvisibleComponent {
	Activity activity;
	Context context;
	int textColor = -4144960;
	int textColorSelect = -16777216;
	int textSize = 15;
	int textSizeSelect = 18;
	int indicatorColor = -13382401;
	int offset = 2;

	public KevinkunWheelView(ComponentContainer container) {
		super(container.$form());
		this.activity = container.$context();
		this.context = container.$context();
	}

	@SimpleEvent
	public void AfterChoose(String choice, int index, String id) {
		EventDispatcher.dispatchEvent(this, "AfterChoose", new Object[] { id, Integer.valueOf(index), choice });
	}

	@SimpleFunction(description = "")
	public void ShowInLayout(HVArrangement layout, List choices, int defaultIndex, final String id) {
		List<String> list = new ArrayList<String>();
		for (int i = 1; i <= choices.size(); i++) {
			list.add(choices.get(i).toString());
		}
		LinearLayout out = new LinearLayout(this.context);
		out.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
		out.setGravity(1);
		WheelView wv = new WheelView(this.context);
		out.addView(wv, -1, -1);
		wv.setOffset(this.offset);

		wv.setTextColor(this.textColor);
		wv.setTextColorSelect(this.textColorSelect);
		wv.setTextSize(this.textSize);
		wv.setTextSizeSelect(this.textSizeSelect);
		wv.setIndicatorColor(this.indicatorColor);

		wv.setItems(list);
		wv.setSeletion(defaultIndex - 1);
		wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			public void onSelected(int index, String item) {
				KevinkunWheelView.this.AfterChoose(id, Integer.valueOf(index - 1).intValue(), item);
			}
		});
		ViewGroup vGroup = (ViewGroup) layout.getView();
		vGroup.removeAllViews();
		vGroup.addView(out, -1, -1);
	}

	@SimpleProperty
	public void ItemColor(int color) {
		this.textColor = color;
	}

	@SimpleProperty
	public void ItemColorSelected(int color) {
		this.textColorSelect = color;
	}

	@SimpleProperty
	public void IndicatorColor(int color) {
		this.indicatorColor = color;
	}

	@SimpleProperty
	public void ItemSize(int size) {
		this.textSize = size;
		this.textSizeSelect = size;
	}

	@SimpleProperty
	public void ItemCount(int count) {

		this.offset = (count - 1) / 2;

	}

	public void ItemSizeSelected(int size) {
		this.textSizeSelect = size;
	}
}
